﻿@{
    ViewBag.Title = this.Context.Request.QueryString["title"];
    Layout = "~/Views/Shared/_PageLayout.cshtml";
}

@section scripts
{
    @Scripts.Render("~/Scripts/swfupload")
    <script type="text/javascript">

        //挂接ExtJS准备完毕事件
        Ext.onReady
        (
            function () {
                //初始化快速提示
                Ext.QuickTips.init();

                //设置状态保存提供者（当前设置为Cookie）
                Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', { path: _StatePath }));

                //用于记录当前记录
                var _CurrentRecord = undefined;

                //创建数据存储对象
                var store = Ext.create
                (
                    'Ext.data.Store',
                    {
                        pageSize: 20,
                        autoSync: false,
                        autoLoad: false,
                        root: 'data',
                        remoteFilter: true,
                        remoteSort: true,
                        sorters: [{ property: 'ActionType_Code', direction: 'ASC' }],
                        model: 'ActionType',
                        listeners:
                        {
                            //处理数据加载事件
                            load: function (store, records, successful, eOpts) {
                                //获得当前选中行
                                var models = grid.getSelectionModel().getSelection();

                                //判断是否选中数据
                                if (models.length > 0) {
                                    //设置当前选中记录
                                    _CurrentRecord = store.getById(models[0].getId());
                                }
                                else {
                                    //设置当前选中记录
                                    _CurrentRecord = null;
                                }
                                //更新按钮状态
                                UpdateButtonState(_CurrentRecord);
                            }
                        },
                        proxy:
                        {
                            type: 'ajax',
                            batchActions: false,
                            url: spms.url.Action('List', 'ActionType'),
                            reader:
                            {
                                type: 'json',
                                root: 'data'
                            },
                            listeners:
                            {
                                //处理代理异常
                                exception: spms.exception.HandleProxyError
                            }
                        }
                    }
                );


                //创建工具栏
                var toolbar = Ext.create
                (
                    'Ext.toolbar.Toolbar',
                    {
                        region: 'north',
                        layout: 'hbox',
                        border: 5,
                        height: 32,
                        border: '1 0 1 0',
                        margins: '0 0 0 0',
                        padding: '0 6 0 6',
                        items:
                        [
                            {
                                id: 'spms-cmd-refresh',
                                text: '刷新',
                                tooltip: '重新加载数据',
                                tooltipType: 'qtip',
                                iconCls: 'spms-icon-refresh',
                                cls: 'spms-page-menu-button',
                                listeners:
                                {
                                    click: OnRefresh
                                }
                            },
                            '-',
                            {
                                id: 'spms-cmd-add',
                                text: '新增',
                                tooltip: '新增操作',
                                tooltipType: 'qtip',
                                cls: 'spms-page-menu-button',
                                iconCls: 'spms-icon-add',
                                listeners:
                                {
                                    click: OnAdd
                                }
                            },
                            {
                                id: 'spms-cmd-remove',
                                text: '删除',
                                tooltip: '删除选中的操作',
                                tooltipType: 'qtip',
                                cls: 'spms-page-menu-button',
                                iconCls: 'spms-icon-delete',
                                disabled: true,
                                listeners:
                                {
                                    click: OnRemove
                                }
                            },
                            {
                                id: 'spms-cmd-edit',
                                text: '编辑',
                                tooltip: '编辑选中的操作',
                                tooltipType: 'qtip',
                                cls: 'spms-page-menu-button',
                                iconCls: 'spms-icon-edit',
                                disabled: true,
                                listeners:
                                {
                                    click: OnEdit
                                }
                            }
                        ]
                    }
                );

                //创建检索面板
                var search = Ext.create
                (
                    'Ext.panel.Panel',
                    {
                        region: 'north',
                        frame: false,
                        animCollapse: false,
                        iconCls: 'spms-icon-search',
                        stateful: true,
                        stateId: 'spms-actiontype-search-state',
                        collapsible: true,
                        collapseMode: 'header',
                        titleCollapse: true,
                        collapsed: true,
                        margin: '0 0 6 0',
                        layout: 'fit',
                        id: 'spms-panel-search',
                        cls: 'spms-page-card-detail',
                        border: true,
                        title: '检索',
                        listeners:
                        {
                            //挂接显示事件
                            show: function (win, options) {
                                //获得表单
                                var form = win.down('form');

                                //判断是否存在
                                if (form) {
                                    //重置内容
                                    form.getForm().reset();

                                    //设置焦点
                                    form.down('field').focus(false, 50);
                                }
                            }
                        },
                        items:
                        [
                            {
                                xtype: 'form',
                                frame: false,
                                autoScroll: true,
                                bodyPadding: 6,
                                bodyCls: 'spms-page-card-detail-form-body',
                                bodyBorder: false,
                                border: false,
                                layout:
                                {
                                    type: 'table',
                                    columns: 2,
                                    tableAttrs:
                                    {
                                        style:
                                        {
                                            width: '100%'
                                        },
                                        cellpadding: 6
                                    }
                                },
                                items:
                                [
                                    {
                                        xtype: 'fieldcontainer',
                                        width: '100%',
                                        defaultType: 'textfield',
                                        layout:
                                        {
                                            type: 'hbox',
                                            align: 'stretch'
                                        },
                                        padding: 0,
                                        border: 0,
                                        items:
                                        [
                                            {
                                                fieldLabel: '',
                                                minWidth: 80,
                                                maxLength: 20,
                                                xtype: 'checkbox',
                                                boxLabel: '操作编码:',
                                                cls: 'spms-card-checkbox',
                                                boxLabelCls: 'spms-card-checkbox-boxlabel',
                                                itemId: 'chkActionType_Code',
                                                allowBlank: true,
                                                value: true,
                                                inputValue: true,
                                                listeners:
                                                {
                                                    change: spms.event.OnCheckBoxLabelChange
                                                }
                                            },
                                            {
                                                fieldLabel: '',
                                                itemId: 'ActionType_Code',
                                                flex: 1
                                            }
                                        ]
                                    },
                                    {
                                        xtype: 'fieldcontainer',
                                        width: '100%',
                                        defaultType: 'textfield',
                                        layout:
                                        {
                                            type: 'hbox',
                                            align: 'stretch'
                                        },
                                        padding: 0,
                                        border: 0,
                                        items:
                                        [
                                            {
                                                fieldLabel: '',
                                                minWidth: 80,
                                                maxLength: 20,
                                                xtype: 'checkbox',
                                                boxLabel: '操作名称:',
                                                cls: 'spms-card-checkbox',
                                                boxLabelCls: 'spms-card-checkbox-boxlabel',
                                                itemId: 'chkActionType_Name',
                                                allowBlank: true,
                                                value: true,
                                                inputValue: true,
                                                listeners:
                                                {
                                                    change: spms.event.OnCheckBoxLabelChange
                                                }
                                            },
                                            {
                                                fieldLabel: '',
                                                itemId: 'ActionType_Name',
                                                flex: 1
                                            }
                                        ]
                                    }
                                ],
                                dockedItems:
                                [
                                    {
                                        xtype: 'toolbar',
                                        dock: 'bottom',
                                        ui: 'footer',
                                        cls: 'spms-page-card-toolbar',
                                        padding: '0 6 12 0',
                                        items:
                                        [
                                            '->',
                                            {
                                                text: '检索',
                                                xtype: 'button',
                                                cls: 'spms-page-card-detail-toolbar-button',
                                                handler: function () {
                                                    //获得当前对象
                                                    var me = this;

                                                    //获得表单对象
                                                    var form = me.up('form');

                                                    //检查数据是否合法
                                                    if (form.getForm().isValid()) {
                                                        //清除过滤条件
                                                        store.filters.clear();

                                                        //生成过滤条件
                                                        var filters = new Array();

                                                        //判断是否以账户名检索
                                                        if (form.down('#chkActionType_Code').getValue()) {
                                                            //添加检索条件
                                                            filters.push({ property: 'ActionType_Code@C', value: form.down('#ActionType_Code').getValue() });
                                                        }

                                                        //判断是否以职员名检索
                                                        if (form.down('#chkActionType_Name').getValue()) {
                                                            //添加检索条件
                                                            filters.push({ property: 'ActionType_Name@C', value: form.down('#ActionType_Name').getValue() });
                                                        }
                                                        //刷新数据
                                                        Refresh({ filters: filters });
                                                    }
                                                }
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                );

                //创建表格
                var grid = Ext.create
                (
                    'Ext.grid.Panel',
                    {
                        region: 'center',
                        store: store,
                        stateful: true,
                        border: true,
                        cls: 'spms-grid',
                        stateId: 'spms-actiontype-grid-state',
                        id: 'spms-card-grid',
                        forceFit: true,
                        autoScroll: true,
                        columnLines: true,
                        enableLocking: true,
                        unlockText: spms.resources.text_grid_column_unlock,
                        lockText: spms.resources.text_grid_column_lock,
                        viewConfig:
                        {
                            stripeRows: true,
                            loadingText: spms.resources.text_loading,
                            singleSelect: true
                        },
                        bbar: Ext.create
                            (
                            'SPMS.PagingToolbar',
                            {
                                store: store
                            }
                        ),
                        listeners:
                        {
                            select: OnRowSelect
                        },
                        columns:
                        [
                            {
                                text: '操作编码',
                                width: 220,
                                minWidth: 220,
                                sortable: true,
                                align: 'left',
                                dataIndex: 'ActionType_Code',
                                hideable: false,
                                lockable: true
                            },
                             {
                                 text: '操作名称',
                                 width: 220,
                                 minWidth: 220,
                                 sortable: true,
                                 align: 'left',
                                 dataIndex: 'ActionType_Name',
                                 hideable: false,
                                 lockable: true
                             },
                             {
                                 text: '备  注',
                                 width: 400,
                                 minWidth: 400,
                                 sortable: false,
                                 align: 'left',
                                 dataIndex: 'Memo',
                                 hideable: true,
                                 flex: 1
                             }
                        ]
                    }
                );
                //创建布局
                var card = Ext.create
                (
                    'Ext.panel.Panel',
                    {
                        region: 'center',
                        layout: 'card',
                        border: false,
                        defaults:
                        {
                            border: false
                        },
                        bodyStyle: 'padding:12px',
                        items:
                        [
                            {
                                xtype: 'panel',
                                id: 'spms-card-list',
                                layout: 'border',
                                items:
                                [
                                    search,
                                    grid
                                ]
                            }
                        ]
                    }
                );

                //创建视口
                var viewport = Ext.create
                (
                    'Ext.container.Viewport',
                    {
                        layout: 'border',
                        items:
                        [
                            {
                                region: 'north',
                                contentEl: 'spms-page-title',
                                border: false,
                                height: 48,
                                margins: '0 0 0 0'
                            },
                            toolbar,
                            card
                        ]
                    }
                );

                //加载数据
                store.load();

                //更新按钮状态

                function UpdateButtonState(record) {
                    //判断是否选中记录
                    if (record) {
                        //设置删除按钮状态
                        spms.common.SetEnabled('spms-cmd-remove', true);
                        //设置编辑按钮状态
                        spms.common.SetEnabled('spms-cmd-edit', true);
                    }
                    else {
                        //设置删除按钮状态
                        spms.common.SetEnabled('spms-cmd-remove', false);
                        //设置编辑按钮状态
                        spms.common.SetEnabled('spms-cmd-edit', true);
                    }
                }

                //处理刷新按钮点击事件
                function OnRefresh(sender) {
                    //刷新数据
                    Refresh();
                }

                //处理新增按钮点击事件
                function OnAdd(sender) {
                    //获得当前对象
                    var me = this;

                    //判断窗体是否已经初始化
                    if (typeof me._Panel_Create == 'undefined') {
                        me._Panel_Create = Ext.create
                       (
                           'Ext.panel.Panel',
                           {
                               layout: 'border',
                               id: 'spms-card-create',
                               cls: 'spms-page-card-detail',
                               border: false,
                               header:
                               {
                                   title: '新增操作',
                                   border: false,
                                   height: 36,
                                   cls: 'spms-page-card-detail-header'
                               },
                               listeners:
                               {
                                   //挂接显示事件
                                   show: function (win, options) {
                                       //获得表单
                                       var form = win.down('form');

                                       //判断是否存在
                                       if (form) {
                                           //获得窗体
                                           form.getForm().reset();

                                           //设置焦点
                                           form.down('field').focus(false, 50);
                                       }
                                   }
                               },
                               items:
                               [
                                   {
                                       xtype: 'form',
                                       region: 'center',
                                       autoScroll: true,
                                       frame: false,
                                       method: 'POST',
                                       waitMsgTarget: true,
                                       waitTitle: spms.resources.text_app_title,
                                       bodyPadding: 12,
                                       bodyCls: 'spms-page-card-detail-form-body',
                                       bodyBorder: false,
                                       border: false,
                                       defaultType: 'textfield',
                                       fieldDefaults:
                                       {
                                           labelAlign: 'left',
                                           labelWidth: 72,
                                           anchor: '100%',
                                           msgTarget: 'qtip'
                                       },
                                       items:
                                       [
                                           {
                                               xtype: 'textfield',
                                               name: 'ActionType_Code',
                                               maxLength: 40,
                                               minLength: 4,
                                               fieldLabel: '操作编码',
                                               emptyText: '操作编码',
                                               allowBlank: false,
                                               disabled: false
                                           },
                                           {
                                               xtype: 'textfield',
                                               name: 'ActionType_Name',
                                               maxLength: 40,
                                               minLength: 4,
                                               fieldLabel: '操作名称',
                                               emptyText: '操作名称',
                                               allowBlank: true,
                                               disabled: false
                                           },
                                           {
                                               xtype: 'textareafield',
                                               name: 'Memo',
                                               fieldLabel: '备　　注',
                                               emptyText: '备注信息（最长400个字符）',
                                               maxLength: 400,
                                               height: 120,
                                               allowBlank: true,
                                               disabled: false
                                           }
                                       ],
                                       dockedItems:
                                       [
                                           {
                                               xtype: 'toolbar',
                                               dock: 'bottom',
                                               ui: 'footer',
                                               cls: 'spms-page-card-detail-toolbar',
                                               padding: '15 0 3 0',
                                               items:
                                               [
                                                   '->',
                                                   {
                                                       text: '连续新增',
                                                       xtype: 'button',
                                                       cls: 'spms-page-card-detail-toolbar-button',
                                                       handler: function () {
                                                           //执行新增操作
                                                           DoAdd.call(me, true);
                                                       }
                                                   },

                                                   {
                                                       text: '新增',
                                                       xtype: 'button',
                                                       cls: 'spms-page-card-detail-toolbar-button',
                                                       handler: function () {
                                                           //执行新增操作
                                                           DoAdd.call(me, false);
                                                       }
                                                   },
                                                   {
                                                       text: '取消',
                                                       xtype: 'button',
                                                       cls: 'spms-page-card-detail-toolbar-button',
                                                       handler: function () {
                                                           //启用工具栏
                                                           toolbar.setDisabled(false);

                                                           //返回一览标签
                                                           card.getLayout().setActiveItem('spms-card-list');
                                                       }
                                                   }
                                               ]
                                           }
                                       ]
                                   }
                               ]
                           }
                       );

                        //添加到面板
                        card.add(me._Panel_Create);
                    }

                    //禁用工具栏
                    toolbar.setDisabled(true);

                    //切换到面板
                    card.getLayout().setActiveItem('spms-card-create');
                }

                //处理删除按钮点击事件
                function OnRemove(sender) {
                    //判断是否选中记录
                    if (!spms.common.IsValid(_CurrentRecord)) {
                        return;
                    }

                    //询问是否确定删除
                    Ext.Msg.show
                    (
                        {
                            title: spms.resources.text_app_title,
                            msg: '确定要删除选中的操作"' + _CurrentRecord.get('ActionType_Name') + '"吗?',
                            buttons: Ext.Msg.YESNO,
                            icon: Ext.Msg.QUESTION,
                            autoScroll: true,
                            closeAction: 'destroy',
                            fn: function (button, text, eOpts) {
                                //判断是否确定删除 
                                if (button == 'yes') {
                                    //创建请求对象
                                    Ext.Ajax.request
                                    (
                                        {
                                            url: spms.url.Action('Delete', 'ActionType'),
                                            method: spms.constant.HttpMethod.POST,
                                            jsonData: _CurrentRecord.data,
                                            callback: function (options, success, response) {
                                                //处理错误
                                                if (spms.exception.HandleAjaxRequestError(options, success, response)) {
                                                    //刷新数据
                                                    Refresh();
                                                }
                                            }
                                        }
                                    );
                                }
                            }
                        }
                    );
                }

                //处理编辑按钮点击事件
                function OnEdit(sender) {
                    //判断是否选中记录
                    if (!spms.common.IsValid(_CurrentRecord)) {
                        //如果没有选中记录，则直接返回
                        return;
                    }

                    //获得当前对象
                    var me = this;

                    //判断窗体是否已经初始化
                    if (typeof me._Panel_Edit == 'undefined') {
                        //创建新增面板
                        me._Panel_Edit = Ext.create
                        (
                            'Ext.panel.Panel',
                            {
                                layout: 'fit',
                                id: 'spms-card-edit',
                                cls: 'spms-page-card-detail',
                                border: false,
                                header:
                                {
                                    title: '编辑操作',
                                    border: false,
                                    height: 36,
                                    cls: 'spms-page-card-detail-header'
                                },
                                listeners:
                                {
                                    //挂接显示事件
                                    show: function (panel, options) {
                                        //获得面板
                                        var panel = me._Panel_Edit;

                                        //获得表单
                                        var form = panel.down('form');

                                        //判断是否存在
                                        if (spms.common.IsValid(form)) {
                                            //重置内容
                                            form.getForm().reset();

                                            //提交数据
                                            form.load
                                            (
                                                {
                                                    url: spms.url.Action('Detail', 'ActionType'),
                                                    method: spms.constant.HttpMethod.Get,
                                                    params: { ActionType_ID: _CurrentRecord.get('ActionType_ID'), ActionType_Name: _CurrentRecord.get('ActionType_Name') },
                                                    waitTitle: spms.resources.text_loading_title,
                                                    waitMsg: spms.resources.text_loading,
                                                    success: function (panel, action) {
                                                        //处理错误
                                                        if (spms.exception.HandleSuccessError(panel, action)) {

                                                        }
                                                        else {
                                                            //启用工具栏
                                                            toolbar.setDisabled(false);

                                                            //返回一览标签
                                                            card.getLayout().setActiveItem('spms-card-list');
                                                        }
                                                    },
                                                    failure: function (form, action) {
                                                        //处理错误
                                                        spms.exception.HandleFailureError(form, action);

                                                        //启用工具栏
                                                        toolbar.setDisabled(false);

                                                        //返回一览标签
                                                        card.getLayout().setActiveItem('spms-card-list');
                                                    }
                                                }
                                            );
                                        }
                                    }
                                },
                                items:
                                [
                                    {
                                        xtype: 'form',
                                        autoScroll: true,
                                        frame: false,
                                        method: 'POST',
                                        waitMsgTarget: true,
                                        waitTitle: spms.resources.text_app_title,
                                        bodyPadding: 12,
                                        bodyCls: 'spms-page-card-detail-form-body',
                                        bodyBorder: false,
                                        border: false,
                                        defaultType: 'textfield',
                                        fieldDefaults:
                                        {
                                            labelAlign: 'left',
                                            labelWidth: 72,
                                            anchor: '100%',
                                            msgTarget: 'qtip'
                                        },
                                        items:
                                        [
                                            {
                                                xtype: 'hiddenfield',
                                                name: 'ActionType_ID'
                                            },
                                            {
                                                xtype: 'textfield',
                                                name: 'ActionType_Code',
                                                maxLength: 40,
                                                minLength: 4,
                                                fieldLabel: '操作编码',
                                                emptyText: '操作编码',
                                                allowBlank: false,
                                                disabled: false
                                            },
                                            {
                                                xtype: 'textfield',
                                                name: 'ActionType_Name',
                                                maxLength: 40,
                                                fieldLabel: '操作名称',
                                                emptyText: '操作名称',
                                                allowBlank: true,
                                                disabled: false
                                            },
                                            {
                                                xtype: 'textareafield',
                                                name: 'Memo',
                                                fieldLabel: '备　　注',
                                                emptyText: '备注信息（最长400个字符）',
                                                maxLength: 400,
                                                height: 120,
                                                allowBlank: true,
                                                disabled: false
                                            }
                                        ],
                                        dockedItems:
                                        [
                                            {
                                                xtype: 'toolbar',
                                                dock: 'bottom',
                                                ui: 'footer',
                                                cls: 'spms-page-card-detail-toolbar',
                                                padding: '15 0 3 0',
                                                items:
                                                [
                                                    '->',
                                                    {
                                                        text: '保存',
                                                        xtype: 'button',
                                                        cls: 'spms-page-card-detail-toolbar-button',
                                                        handler: function () {
                                                            //获得表单
                                                            var form = me._Panel_Edit.down('form');

                                                            //用于记录需要提交的数据
                                                            var postParams = [];

                                                            //判断表单是否有效
                                                            if (form) {
                                                                //获得表单对象
                                                                var formBase = form.getForm();

                                                                //检查数据是否合法
                                                                if (formBase.isValid()) {
                                                                    //提交数据
                                                                    formBase.submit
                                                                    (
                                                                        {
                                                                            url: spms.url.Action('Update', 'ActionType'),
                                                                            method: spms.constant.HttpMethod.Post,
                                                                            params: { Roles: postParams },
                                                                            submitEmptyText: false,
                                                                            waitTitle: spms.resources.text_submitting_title,
                                                                            waitMsg: spms.resources.text_submitting,
                                                                            success: function (panel, action) {
                                                                                //处理提交错误
                                                                                if (spms.exception.HandleSuccessError(panel, action)) {
                                                                                    //启用工具栏
                                                                                    toolbar.setDisabled(false);

                                                                                    //返回一览标签
                                                                                    card.getLayout().setActiveItem('spms-card-list');

                                                                                    //刷新数据
                                                                                    Refresh();
                                                                                }
                                                                            },
                                                                            failure: spms.exception.HandleFailureError
                                                                        }
                                                                    );
                                                                }
                                                            }
                                                        }
                                                    },
                                                    {
                                                        text: '取消',
                                                        xtype: 'button',
                                                        cls: 'spms-page-card-detail-toolbar-button',
                                                        handler: function () {
                                                            //启用工具栏
                                                            toolbar.setDisabled(false);

                                                            //返回一览标签
                                                            card.getLayout().setActiveItem('spms-card-list');
                                                        }
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            }
                        );

                        //添加到面板
                        card.add(me._Panel_Edit);
                    }

                    //禁用工具栏
                    toolbar.setDisabled(true);

                    //切换到面板
                    card.getLayout().setActiveItem('spms-card-edit');
                }

                //刷新数据
                function Refresh(options) {
                    //刷新数据
                    store.load(options);
                }

                //处理表格行选中事件
                function OnRowSelect(panel, record, index, options) {
                    //记录当前选中记录
                    _CurrentRecord = record;

                    //更新按钮状态
                    UpdateButtonState(record);
                }

                //执行新增操作
                function DoAdd(isContinuousNew) {
                    //获得当前对象
                    var me = this;

                    //获得表单
                    var form = me._Panel_Create.down('form');

                    //用于记录需要提交的数据
                    var postParams = [];

                    //判断表单是否有效
                    if (form) {
                        //获得表单对象
                        var formBase = form.getForm();

                        //检查数据是否合法
                        if (formBase.isValid()) {
                            //提交数据
                            formBase.submit
                            (
                                {
                                    url: spms.url.Action('Create', 'ActionType'),
                                    method: spms.constant.HttpMethod.Post,
                                    params: { Roles: postParams },
                                    submitEmptyText: false,
                                    waitTitle: spms.resources.text_submitting_title,
                                    waitMsg: spms.resources.text_submitting,
                                    success: function (panel, action) {
                                        //处理提交错误
                                        if (spms.exception.HandleSuccessError(panel, action)) {
                                            //判断是否连续新增
                                            if (isContinuousNew) {
                                                //重置表单
                                                panel.reset();

                                                //设置焦点
                                                form.down('field').focus(false, 50);
                                            }
                                            else {
                                                //启用工具栏
                                                toolbar.setDisabled(false);

                                                //返回一览标签
                                                card.getLayout().setActiveItem('spms-card-list');

                                                //刷新数据
                                                Refresh();
                                            }
                                        }
                                    },
                                    failure: spms.exception.HandleFailureError
                                }
                            );
                        }
                    }
                }
            }
        );
    </script>
}
